<?php
class AdminModel extends ModelBase
{
	private static $_tableName = 'admin_user';
	
	public static $groups = array(
		'super'	=> '超级管理员',
		'admin'	=> '管理员',
		'author'=> '信息发布员'
	);
	
	public static function add($uname, $passwd, $alias, $group='admin', $permission='')
	{
		if (empty($uname) || empty($passwd) || empty($alias)){
			return false;
		}
		$data = array(
			'uname'	=> $uname,
			'passwd'	=> md5($passwd),
			'alias'		=> $alias,
			'group'		=> $group ? $group : 'admin',
			'permission'=> $permission ? json_encode($permission) : '',
		);
		return self::queryInsert(self::$_tableName, $data);
	}
	
	public static function get($uid='', $uname='')
	{
		if (empty($uid) && empty($uname)){
			return false;
		}
		$where = '';
		if ($uid){
			$where = "id={$uid}";
		}else{
			$where = "uname='{$uname}'";
		}
		$ret = self::querySelectSingle(self::$_tableName, array('*'), $where);
		if ($ret){
			$ret['permission'] = $ret['permission'] ? json_encode($ret['permission'], true) : '';
		}
		return $ret;
	}
	
	public static function getList($where='')
	{
		return self::querySelect(self::$_tableName, array('*'), $where);
	}
	
	public static function update($uid, $data)
	{
		if(empty($uid) || empty($data)){
			return false;
		}
		if (isset($data['permission']) && $data['permission']){
			$data['permission'] = json_encode($data['permission']);
		}
		$where = "id={$uid}";
		return self::queryUpdate(self::$_tableName, $data, $where);
	}
	
	public static function delete($uid)
	{
		$where = "id={$uid}";
		return self::queryDelete(self::$_tableName, $where);
	}
}